//1. 安装 mongoose
//2. 引入 mongoose 模块
const mongoose = require('mongoose');

//3. 连接mongodb数据库
mongoose.connect('mongodb://127.0.0.1:27017/project', { useNewUrlParser: true,useUnifiedTopology: true } );

//4. 设置连接成功的回调
mongoose.connection.on('open', () => {
    //5. 创建文档结构对象
    const ComputerSchema = new mongoose.Schema({
        brand: String,
        price: Number,
        color: String
    });
    //6. 创建文档模型对象
    const ComputerModel = mongoose.model('computers', ComputerSchema);

    //7. 文档插入 insertMany  updateOne  remove  find
    ComputerModel.create({
        brand: 'ThinkPad',
        price: 4299,
        color: '高级黑'
    }, (err, data) => {
        if(err) throw err;
        console.log(data);
        //8. 关闭数据库连接
        mongoose.connection.close();
    });
});